package com.cytangaa.youtubedownload;

import java.io.InputStream;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.cytanaa.youlike.R;
import com.cytangaa.searchyoutube.SearchYoutube;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

public class DownloadFromMyServer extends AsyncTask<String, Void, Object> {
	private String link;
	private List<Map<String, Object>> mData = null;
	private SearchYoutube mySearchYoutube;
	private String[] videoInfo = new String[100];
	private int arrayLength;
	public Context ctx;
	private Bitmap[] thumbnail = new Bitmap[10];

	public DownloadFromMyServer(String link, Context ctx) {
		this.link = link;
		this.ctx = ctx;
	}

	@Override
	protected Object doInBackground(String... params) {
		// TODO Auto-generated method stub
		String jsonString = mySearchYoutube.YoutubeResult(link);
		Log.i("in AsycTask", "link=  " + link);
		Log.i("in AsycTask", "jsonString=  " + jsonString);
		videoInfo = mySearchYoutube.MyJSONToString(jsonString);
		Log.i("in AsycTask", "1");
		try {
			for (int j = 4, i = 0; j < arrayLength; j = j + 9, i++) {
				Log.i("Check bitmap", "before, j = " + j + "  ,i = " + i);
				Log.i("Check bitmap", "after" + videoInfo[j]);
				try {
					thumbnail[i] = BitmapFactory
							.decodeStream((InputStream) new URL(videoInfo[j])
									.getContent());
				} catch (Exception e) {
					thumbnail[i] = BitmapFactory.decodeResource(
							this.ctx.getResources(), R.drawable.video_default);
				}
			}
			return "not null";
		} catch (Exception anyError) {
			return null;
		}
	}

	protected void onPostExecute(Object result) {
		// Pass the result data back to the main activity
		// Login.this.data = result;
		Object checkNull = result;
		if (checkNull != null) {
			mData = getData(mData);
			ReturnMethod();

		}
		ReturnMethod();
	}

	public List<Map<String, Object>> ReturnMethod() {
		// handle value
		return mData;
	}

	private List<Map<String, Object>> getData(List<Map<String, Object>> mList) {

		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		if (mList != null) {
			list = mList;
		}
		Log.v("In videoInfo List", "length: " + arrayLength);

		Map<String, Object> map;

		for (int j = 0, i = 0; j < arrayLength; j++, i++) {
			map = new HashMap<String, Object>();

			map.put("video_id", videoInfo[j]);
			j++;

			Date d2 = new Date();
			// videoInfo[j]= videoInfo[j].replaceAll("Z","+0800");
			SimpleDateFormat dateformat1 = new SimpleDateFormat("yyyy-MM-dd");
			Date youtubeDate = new Date();
			try {
				Log.v("checking", videoInfo[j]);
				youtubeDate = dateformat1.parse(videoInfo[j]);
				Log.v("In videoInfo List", "youtubeDate: " + youtubeDate);
			} catch (ParseException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			String time = "";
			int year, month, day, year2, month2, day2 = 0;
			// int hour, minute, seccond, hour2, minute2, seccond2 =0;
			Calendar calendar = Calendar.getInstance();
			Calendar calendar2 = Calendar.getInstance();
			calendar.setTime(youtubeDate);
			calendar2.setTime(d2);

			year = calendar.get(Calendar.YEAR);
			;
			year2 = calendar2.get(Calendar.YEAR);
			month = calendar.get(Calendar.MONTH);
			month2 = calendar2.get(Calendar.MONTH);
			day = calendar.get(Calendar.DAY_OF_MONTH);
			day2 = calendar2.get(Calendar.DAY_OF_MONTH);
			// hour = calendar.get(Calendar.HOUR_OF_DAY);
			// hour2 = calendar2.get(Calendar.HOUR_OF_DAY);
			// minute = calendar.get(Calendar.MINUTE);
			// minute2 = calendar2.get(Calendar.MINUTE);

			if (year2 - year > 0) {
				time = (year2 - year) + " years ago | ";
				map.put("uploaded", time);
				Log.v("In videoInfo List", "youtubeDate: " + "years");

			} else if (month - month > 0) {
				time = (month2 - month) + " months ago | ";
				map.put("uploaded", time);
				Log.v("In videoInfo List", "youtubeDate: " + "months");

			} else if (day2 - day > 0) {
				time = (day2 - day) + " days ago | ";
				map.put("uploaded", time);
				Log.v("In videoInfo List", "youtubeDate: " + "days");

			} else {
				map.put("uploaded", "uploaded today | ");
				Log.v("In videoInfo List", "youtubeDate: " + "today");

			}
			map.put("uploaded_date", videoInfo[j]);
			Log.v("In videoInfo List", "uploaded_date: " + videoInfo[j]);

			// map.put("uploaded", videoInfo[j]);
			j++;
			map.put("category", videoInfo[j]);
			j++;
			map.put("title", videoInfo[j]);
			j++;
			map.put("thumbnail", thumbnail[i]);
			map.put("thumbnail_link", videoInfo[j]);
			j++;

			float a = Float.parseFloat(videoInfo[j]);
			int b = (int) a;
			int sec = b % 60;
			int min = b / 60;
			String duration;
			if (sec < 10) {
				duration = min + " : 0" + sec;
			} else {
				duration = min + " : " + sec;
			}
			// videoInfo[j] = duration;
			map.put("duration", duration);
			map.put("duration_raw", videoInfo[j]);
			Log.v("In videoInfo List", "duration: " + duration);
			// map.put("duration", videoInfo[j]);
			j++;
			map.put("share_link", videoInfo[j]);
			Log.v("In videoInfo List", "share_link: " + videoInfo[j]);
			j++;
			map.put("rtsp", videoInfo[j]);
			Log.v("In videoInfo List", "rtsp: " + videoInfo[j]);
			j++;
			map.put("view_count", videoInfo[j]);

			list.add(map);

		}

		Log.v("In videoInfo List", "list: " + list.size());

		return list;
	}

}